<template>
  <Modal
    title="编辑"
    :value="value"
    :mask-closable="true"
    :footer-hide="true"
    @input="$emit('input', $event)"
  >
    <Form
      ref="formLkyDocumentTableManage"
      :model="formLkyDocumentTableManage"
      label-position="right"
      :rules="ruleValidate"
      :label-width="80"
    >
      <!--      -->
      <FormItem label="文件名" prop="file_name">
        <Input type="text" v-model="formLkyDocumentTableManage.file_name" placeholder="文件名"/>
      </FormItem>

      <FormItem label="周期" prop="period">
        <Select v-model="formLkyDocumentTableManage.period" style="width:200px">
          <Option v-for="item in periodList" :value="item.value" :key="item.value">
            {{ item.key }}
          </Option>
        </Select>
      </FormItem>

      <FormItem label="需填次数" prop="times">
        <InputNumber v-model="formLkyDocumentTableManage.times" placeholder="需填次数"/>
      </FormItem>
      <FormItem label="表类型" prop="table_type">
        <Select v-model="formLkyDocumentTableManage.table_type" style="width:200px">
          <Option v-for="item in tableTypeList" :value="item.value" :key="item.value">
            {{ item.key }}
          </Option>
        </Select>
      </FormItem>
      <FormItem label="开始时间" prop="start_time">
        <DatePicker v-model="formLkyDocumentTableManage.start_time" type="date" format="yyyy/MM/dd" placeholder="开始时间" style="width: 200px" ></DatePicker>
      </FormItem>
      <FormItem label="结束时间" prop="end_time">
        <DatePicker v-model="formLkyDocumentTableManage.end_time" type="date" format="yyyy/MM/dd" placeholder="结束日期" style="width: 200px" ></DatePicker>
      </FormItem>
      <FormItem>
        <Button type="primary" @click="handleSubmit('formLkyDocumentTableManage')">提交</Button>
        <Button @click="handleReset('formLkyDocumentTableManage')" style="margin-left: 8px">重置</Button>
      </FormItem>
    </Form>
  </Modal>
</template>
<script>
  import { update } from '@/api/lky/lkyDocumentTableManage'
  import { createModelObj, coverProps, coverFormProps } from '@/libs/util'

  export default {
    name: 'Edit',
    props: {
      value: {
        type: Boolean,
        default: false
      },
      lkyDocumentTableManage: {
        type: Object,
        default: null
      }
    },
    data () {
      return {
        periodList: [
          {
            key: "天",
            value: "1"
          },
          {
            key: "周",
            value: "2"
          },
          {
            key: "月",
            value: "3"
          },
          {
            key: "期",
            value: "4"
          },
          {
            key: "年",
            value: "5"
          }
        ],
        tableTypeList: [
          {
            key: "老师发布表",
            value: 1
          },
          {
            key: "学生填写表",
            value: 2
          },
          {
            key: "老师评分表",
            value: 3
          },
          {
            key: "教学工作页",
            value: 4
          }
        ],
        formLkyDocumentTableManage: {
          id: "",
          file_name: ''
          ,
          period: ''
          ,
          times: 0
          ,
          table_type: 0
          ,
          start_time: ''
          ,
          end_time: ''
        },
        ruleValidate: {

          file_name: [
            { required: true, message: '该值不能为空', trigger: 'blur' },
            { type: 'string', max: 20, message: '长度最长为20', trigger: 'blur' }
          ]
          ,
          period: [
            { required: true, message: '该值不能为空', trigger: 'blur', type: 'string', max: 1 }
          ]
          ,
          times: [
            { required: true, message: '该值不能为空', trigger: 'blur',type: 'number' },
          ]
          ,
          table_type: [
            { required: true, message: '该值不能为空', trigger: 'blur', type: 'number'},
          ]
          ,
          start_time: [
            { required: true, message: '该值不能为空', trigger: 'blur',type:'date' },
          ]
          ,
          end_time: [
            { required: true, message: '该值不能为空', trigger: 'blur',type:'date' },
          ]
        }
      }
    },
    methods: {
      handleSubmit (name) {
        this.$refs[name].validate(valid => {
          if (valid) {
            update(
              Object.assign({}, createModelObj(this.formLkyDocumentTableManage, 'lkyDocumentTableManage'))
            ).then(res => {
              if (res) {
                this.$emit('updateRow', coverProps(this.lkyDocumentTableManage, this.formLkyDocumentTableManage))
                this.$emit('input', false)
                this.handleReset(name)
              }
            })
          } else {
            this.$Message.error('验证失败!')
          }
        })
      },
      handleReset (name) {
        this.$refs[name].resetFields()
      },
      changeStartDate(a) {
        console.log(this.formLkyDocumentTableManage)
      }

    },
    watch: {
      value: function (val, oldVal) {
        this.$refs.formLkyDocumentTableManage.resetFields()
        coverFormProps(this.formLkyDocumentTableManage, this.lkyDocumentTableManage)
      }
    }
  }
</script>
